package com.lyxedu.management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lyxedu.management.Services.UserServie;
import com.lyxedu.management.Utils.JwtHelper;
import com.lyxedu.management.handler.R;
import com.lyxedu.management.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/user")
@Api(tags = "登录相关接口")
public class UserController {
    @Resource
    private UserServie userServie;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "管理员账号和密码",required = true)
    })
    public R<String> login(@RequestBody User user){
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,user.getUsername());
        User one = userServie.getOne(wrapper);
        if (user != null){
            if (one.getPassword().equals(user.getPassword())){
//                生成touken
                JwtHelper jwtHelper = new JwtHelper();
                String token = jwtHelper.createToken(one.getId());
                return R.success(token);
            }
        }
        return R.error("登录失败");
    }
}
